package com.example.firstjavamqtt.factory.strategy.callback;

import com.alibaba.fastjson.JSONObject;
import com.example.firstjavamqtt.model.dto.mqtt.MqttTemperatureHumidityDto;
import com.example.firstjavamqtt.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class HandleTempAndHumidityMsgStrategy implements HandleMessageStrategy{

    private static RedisUtil redisUtil;

    @Override
    public void handle(String msg) {
        //存redis
        MqttTemperatureHumidityDto dto = JSONObject.parseObject(msg, MqttTemperatureHumidityDto.class);
        if(dto.getHumidity()==0 || dto.getTemperature() == 0){
            return;
        }
        System.out.println(dto);
        redisUtil.set("TempHumidity:" + dto.getDeviceId(),dto,15);
        //System.out.println("到这");
    }

    @Autowired
    public void setRedisUtil(RedisUtil redisUtil) {
        HandleTempAndHumidityMsgStrategy.redisUtil = redisUtil;
    }

}
